package rpg002;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

/**
 *
 * @author Smai
 */
public class MapEditor {

  Toolkit toolkit = Toolkit.getDefaultToolkit();
  private ActionListener actionListener;
  private Map map;
  private ArrayList<Tile> tilesArray = new ArrayList<Tile>();
  private String type = "water";
  private Image image = toolkit.getImage("Resources//textures//" + type + ".png");
  private int number = 0;
  private String mapName = "map01";

  public MapEditor() {
    readTilesToMemory();
  }

  public void setMap(Map map, ActionListener actionListener) {
    this.actionListener = actionListener;
    this.map = map;
  }

  public Tile setTile() {
    return tilesArray.get(number).clone();
  }

  public void nextTile() {
    if (number >= tilesArray.size() - 1) {
      number = 0;
    } else {
      number++;
    }
    type = tilesArray.get(number).getType();
    image = toolkit.getImage("Resources//textures//" + type + ".png");

  }

  public void prevTile() {
    if (number <= 0) {
      number = tilesArray.size() - 1;
    } else {
      number--;
    }
    type = tilesArray.get(number).getType();
    image = toolkit.getImage("Resources//textures//" + type + ".png");
  }

  public void paint(Graphics gbi) {
    gbi.drawImage(image, 32, 32+320, null);
    gbi.setColor(Color.white);
    gbi.drawRect(31, 31+320, 34, 34);
    gbi.drawString("Current tile (" + (number + 1) + "/" + tilesArray.size() + ")", 72, 48+320);
  }

  public void readTilesToMemory() {

    int posX;
    int posY;
    int width;
    int height;
    String type;
    String info;
    int collisionBoolean;
    boolean collision;

    try {
      System.out.println("Loading tiles to editor...");

      BufferedReader br = new BufferedReader(new FileReader(new File("Resources//Tiles//tiles.txt")));
      String luettuRivi = br.readLine();
      while (luettuRivi != null) {

        String[] pilkottuRivi = luettuRivi.split("\\|");

        posX = Integer.parseInt(pilkottuRivi[0]);
        posY = Integer.parseInt(pilkottuRivi[1]);
        width = Integer.parseInt(pilkottuRivi[2]);
        height = Integer.parseInt(pilkottuRivi[3]);
        type = pilkottuRivi[4];
        info = pilkottuRivi[5];
        collisionBoolean = Integer.parseInt(pilkottuRivi[6]);

        if (collisionBoolean == 1) {
          collision = true;
        } else {
          collision = false;
        }

        tilesArray.add(new Tile(posX, posY, width, height, type, info, collision));
        luettuRivi = br.readLine();

      }
    } catch (FileNotFoundException ex) {
      ex.printStackTrace();
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }

  public void saveMap() {

    if (actionListener.buttonK) {
      Tile[][] worldTiles = map.getWorldTiles();

      try {
        PrintWriter writer = new PrintWriter(new File("Resources//Maps//" + mapName + ".txt"));

        for (int i = 0; i < map.mapSize; i++) {
          for (int j = 0; j < map.mapSize; j++) {
            writer.println(worldTiles[i][j].getSaveInfo());
          }
        }

        writer.close();

      } catch (Exception ex) {
        ex.printStackTrace();
      }
    } else {
      System.out.println("Can't save. Need editor mode.");
    }
  }
}
